﻿Imports System.IO
Imports System.Data.SqlClient

Public Class MainForm
    Private Sub btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Save.Click
        Dim _Cmd As SqlCommand
        Dim _Reader As SqlDataReader
        Dim _TotalCount As Integer
        Dim _SavedCount As Integer = 0
        Dim _ExceptionCount As Integer = 0
        Dim _OutputWriter As StreamWriter = Nothing
        Dim _ExceptionWriter As StreamWriter = Nothing

        Dim _Conn As New SqlConnection("Server=" & txt_Server.Text & ";Database=" & txt_Database.Text & ";Trusted_Connection=True;")

        Try
            _Conn.Open()
        Catch ex As Exception
            MsgBox("Error connecting to SQL Server" & vbCrLf & ex.Message)
            Exit Sub
        End Try

        Try
            'Set up our output and exception CSV files
            If Not txt_OutputPath.Text.EndsWith("\") Then
                txt_OutputPath.Text = txt_OutputPath.Text & "\"
            End If
            _OutputWriter = New StreamWriter(txt_OutputPath.Text & "_Output.csv")
            _ExceptionWriter = New StreamWriter(txt_OutputPath.Text & "_Exceptions.csv")
            _OutputWriter.WriteLine("""ID"",""Filename""")
            _ExceptionWriter.WriteLine("""ID"",""Exception""")
        Catch ex As Exception
            MsgBox("Error setting up Output and Exceptions files!" & vbCr & ex.Message)
            Exit Sub
        End Try

        Try
            'Get count of objects
            _Cmd = New SqlCommand("Select Count(1) From [" & txt_Table.Text & "] Where [" & txt_BlobField.Text & "] Is Not Null", _Conn)
            _Reader = _Cmd.ExecuteReader
            While _Reader.Read
                _TotalCount = _Reader.Item(0)
            End While
            _Reader.Close()
        Catch ex As Exception
            MsgBox("Error Getting Object Count" & vbCrLf & ex.Message)
            Exit Sub
        End Try

        Try
            _Cmd = New SqlCommand("Select * From [" & txt_Table.Text & "]", _Conn)
            _Reader = _Cmd.ExecuteReader

            While _Reader.Read
                Try
                    Dim _OLEReader As New OLEReader(_Reader.Item(txt_BlobField.Text))
                    Dim _SavedFilename As String
                    _SavedFilename = _OLEReader.SaveFile(txt_OutputPath.Text, _Reader.Item(txt_IDField.Text), chk_Dat.Checked)
                    _OutputWriter.WriteLine("""" & _Reader.Item(txt_IDField.Text) & """,""" & _SavedFilename & """")
                    _SavedCount = _SavedCount + 1
                    _OLEReader = Nothing
                Catch ex As Exception
                    _ExceptionWriter.WriteLine("""" & _Reader.Item(txt_IDField.Text) & """,""" & ex.Message & """")
                    _ExceptionCount = _ExceptionCount + 1
                End Try
                lbl_Counts.Text = _SavedCount + _ExceptionCount & "/" & _TotalCount
                Application.DoEvents()
            End While
            _Reader.Close()
            _Conn.Close()
            _OutputWriter.Close()
            _ExceptionWriter.Close()

            MsgBox("Complete!", MsgBoxStyle.OkOnly)

        Catch ex As Exception
            MsgBox("Error Getting Media" & vbCrLf & ex.Message)
        End Try

    End Sub

    Private Sub btn_Browse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Browse.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            txt_OutputPath.Text = FolderBrowserDialog1.SelectedPath
        End If
    End Sub
End Class
